package org.csu.petserver.web;


import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.PositiveOrZero;
import org.apache.ibatis.annotations.Param;
import org.csu.petserver.domain.Category;
import org.csu.petserver.domain.Inventory;
import org.csu.petserver.domain.Item;
import org.csu.petserver.domain.Product;
import org.csu.petserver.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@SessionAttributes(value = {"sessionitem","categoryId"})
public class CatalogController {
    @Autowired
    CategoryInfo categoryInfo;

    @Autowired
    ProductInfo productInfo;

    @Qualifier("ItemInfo")
    @Autowired
    ItemInfo itemInfo;

    @Autowired
    InventoryInfo inventoryInfo;

//    @GetMapping("/catalog/allproduct")
//    String viewall(Model model){
//        List<Category> categories =categoryInfo.getAll();
//        model.addAttribute("categories",categories);
//        return "catalog/viewcategories";
//    }

    @GetMapping("viewProductAttr")
    String viewProductByCategory(@RequestParam String categoryId, Model model){
        List<Product> products=categoryInfo.getAllProductByCategoryId(categoryId);
        model.addAttribute("products",products);
        model.addAttribute("categoryId",categoryId);
        return "productAttr";
    }



    //为空显示 在前端表示！！！！！！
    @GetMapping("viewProduct")
    String viewItemByProduct(Model model){
        List<Item> items=productInfo.getAllItem();
        model.addAttribute("items",items);
        return "product";
    }

//    @GetMapping("/catalog/{itemInfo")
//    String viewItemInfo(@Param("itemId") String itemId,Model model){
//        Item item=itemInfo.getItem(itemId);
//        model.addAttribute("item",item);
//        return "catalog/viewItemInfo";
//    }
    //判断图片路径是否修改
    @PostMapping("/catalog/updateProduct")
    void updateProduct(@ModelAttribute Product product,Model model){
        productInfo.updateProduct(product);
    }

    @RequestMapping ("/addProductAttr")
    String addProductAttr(@SessionAttribute("categoryId") String categoryId,@ModelAttribute Product product,Model model)
    {
        //productInfo.addProduct(product);
        product.setCategory(categoryId);
        if(productInfo.getProductById(product.getId())==null){
            productInfo.addProduct(product);
        }
        else{
            productInfo.updateProduct(product);
        }
        List<Product> products=categoryInfo.getAllProductByCategoryId(categoryId);
        model.addAttribute("products",products);
        System.out.println("11111111111111111111111111111111111111");
        return "productAttr";
    }

    @RequestMapping("/deleteProductAttr")
    String deleteProductAttr(@SessionAttribute("categoryId") String categoryId,@Param("productId") String productId,Model model){
        productInfo.deleteProduct(productInfo.getProductById(productId));
        List<Product> products=categoryInfo.getAllProductByCategoryId(categoryId);
        model.addAttribute("products",products);
        return "productAttr";
    }

    @PostMapping("/catalog/updateItem")
    void updateItem(@ModelAttribute Item item, Model model){
        itemInfo.updateItem(item);
    }

//    @GetMapping("/catalog/search")
//    @ResponseBody
//    public String search(String keyword){
//        productInfo.searchProductInCertainCategory()
//    }

    @PostMapping("/catalog/updateInventory")
    void updateInventory(@PositiveOrZero @Param("inventory") int inventory,@Param("workingItemId") String itemId){
        inventoryInfo.updateQuantity(itemInfo.getItem(itemId),inventory);
    }
}
